package middleware

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
)

func AdminMiddleware() gin.HandlerFunc {
	return func(c *gin.Context) {
		role, ok := c.Get("role")
		if !ok {
			fmt.Println("Role not found in context")
			c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{
				"error": "无法获取用户角色",
			})
			return
		}
		// 将 role 转换为 int 类型
		roleInt, ok := role.(int)
		if !ok {
			fmt.Println("Role is not an integer")
			c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{
				"error": "无效的用户角色类型",
			})
			return
		}
		fmt.Println("Context Role:", roleInt)
		if roleInt != 1 {
			c.AbortWithStatusJSON(http.StatusForbidden, gin.H{
				"error": "你的身份不支持这个操作",
			})
			return
		}
		c.Next()
	}
}
